<script type="text/javascript" src="admin/newCourse.js"></script>
<script type="text/javascript" src="public/types.js"></script>
<div id="main-newCourse" style="position:absolute;left:23%;">
	<%
	   	String courseName = request.getParameter("courseName");
	   	String courseId = request.getParameter("courseId");
	%>
	<%
		String reason = request.getParameter("reason");
		if ("invalid".equals(reason)) {
			%>
			<h2 class="errorText">One or more of the details were invalid. please try again.</h2>
			<%			
		}
		else if("mySessions".equals(reason)) {
			%>
			<h2 class="errorText">Session details were invalid. please try again.</h2>
			<%			
		}
		else if("1".equals(reason)) {
			%>
			<h2 class="errorText">Sessions to be added cannot overlap.</h2>
			<%			
		}
		else if("2".equals(reason)) {
			%>
			<h2 class="errorText">Other courses from same group number overlap current sessions.</h2>
			<%			
		}
		if(courseName != null) {
			%>
			<h2 class="errorText">Please insert a unique course name. The name you entered already exists.</h2>
			<%
		}
		else if(courseId != null) {
			%>
			<h2 class="errorText">Please insert a unique course id. The id you entered already exists.</h2>
			<%				
		}
	%>
	<form id="newCourseForm" method="post" action="#">
		<div style="text-align:left;">
			<fieldset>
				<legend>Required details:</legend>
				<table>
					<tr>
						<td><label for="courseId">Course id:</label></td>
						<td><input type="text" name="courseId" id="courseId" size="6" maxlength="6"  onkeypress="return isNumberKey(event);" /></td>
						<td id="courseIdText" class="errorText"></td>
					</tr>
					<tr>
						<td><label for="courseName">Course name:</label></td>
						<td><input type="text" name="courseName" id="courseName" maxlength="32" onkeypress="return (isNameKey(event) || isNumberKey(event));" /></td>
						<td id="courseNameText" class="errorText"></td>
					</tr>
					<tr>
						<td><label for="lecturerInCharge">Lecturer in charge:</label></td>
						<td><input type="text" name="lecturerInCharge" id="lecturerInCharge" maxlength="32" onkeypress="return isNameKey(event);" /></td>
						<td id="lecturerInChargeText" class="errorText"></td>
					</tr>
					<tr>
						<td><label for="moedA">Moed A:</label></td>
						<td><input type="text" name="moedA" id="moedA" value="DD.MM.YYYY" size="14" maxlength="10" onclick="deleteCode(this)" onkeypress="return isDateKey(event);" /></td>
						<td id="moedAText" class="errorText"></td>
					</tr>
					<tr>
						<td><label for="moedB">Moed B:</label></td>
						<td><input type="text" name="moedB" id="moedB" value="DD.MM.YYYY" size="14" maxlength="10" onclick="deleteCode(this)" onkeypress="return isDateKey(event);" /></td>
						<td id="moedBText" class="errorText"></td>
					</tr>
					<tr>
						<td><label for="description">Description:</label></td>
						<td>
						<textarea name="description" id="description" rows=1 cols=30 style="overflow:hidden;" ></textarea></td>
						<td id="descriptionText" class="errorText"></td>
					</tr>
					<tr>
						<td><label for="creditPoints">Credit points:</label></td>
						<td>
						<select name="creditPoints" id="creditPoints">
							<option value="1">1</option>
							<option value="1.5">1.5</option>
							<option value="2">2</option>
							<option value="2.5">2.5</option>
							<option value="3">3</option>
							<option value="3.5">3.5</option>
							<option value="4">4</option>
							<option value="4.5">4.5</option>
							<option value="5">5</option>
							<option value="5.5">5.5</option>
						</select>
						</td>
					</tr>
					<tr>
						<td><label for="capacity">Capacity:</label></td>
						<td><input type="text" name="capacity" id="capacity" size="3" maxlength="3" onkeypress="return isNumberKey(event);" /></td>
						<td id="capacityText" class="errorText"></td>
					</tr>
					<tr>
						<td><label for="groupNum">Group number:</label></td>
						<td><input type="text" name="groupNum" id="groupNum"  size="3" maxlength="2" onkeypress="return isNumberKey(event);" /></td>
						<td id="groupNumText" class="errorText"></td>
					</tr>
					<tr>
						<td><label for="numberOfSessions">Studying sessions:</label></td>
						<td>
						<select name="numberOfSessions" id="numberOfSessions" onkeyup="createSessions();" onchange="createSessions();">
							<option value="1" selected="selected">1</option>
							<option value="2">2</option>
							<option value="3">3</option>
							<option value="4">4</option>
							<option value="5">5</option>
							<option value="6">6</option>
							<option value="7">7</option>
							<option value="8">8</option>
							<option value="9">9</option>
							<option value="10">10</option>
						</select>
						</td>
					</tr>
				</table>
				<table id="numberOfSessionsTable"><tbody>
				<%
					for(int i=0; i<10; i++)
					{
				%>
				<tr id="trOfTable<%=i %>" style="display: none;">
					<td>
					<input type="text" name="teacherOfSession<%=i %>" id="teacherOfSession<%=i %>" value="teacher name" size=14 maxlength=32 onclick="deleteCode(this)" onkeypress="return isNameKey(event);" />
					</td>
					<td>
					<input type="text" name="classLocationOfSession<%=i %>" id="classLocationOfSession<%=i %>" value="class location" size=14 maxlength=32 onclick="deleteCode(this)" onkeypress="return (isNameKey(event) || isNumberKey(event));" />
					</td>
					<td>
					day: <select name="dayOfSession<%=i %>" id="dayOfSession<%=i %>">
							<option value="Sunday" selected="selected">Sunday</option>
							<option value="Monday">Monday</option>
							<option value="Tuesday">Tuesday</option>
							<option value="Wednesday">Wednesday</option>
							<option value="Thursday">Thursday</option>
							<option value="Friday">Friday</option>
							<option value="Saturday">Saturday</option>
					</select>
					</td>
					<td>
					type: <select name="typeOfSession<%=i %>" id="typeOfSession<%=i %>">
							<option value="tutorial" selected="selected">tutorial</option>
							<option value="lecture">lecture</option>
					</select>
					</td>
					<td>
					start time: <select name="startOfSession<%=i %>" id="startOfSession<%=i %>" onkeyup="changeDuration(<%=i %>);" onchange="changeDuration(<%=i %>);">            
								<option value="8" selected="selected">08:00</option>
								<option value="9" >09:00</option>
								<option value="10" >10:00</option>
								<option value="11" >11:00</option>
								<option value="12" >12:00</option>
								<option value="13" >13:00</option>
								<option value="14" >14:00</option>
								<option value="15" >15:00</option>
								<option value="16" >16:00</option>
								<option value="17" >17:00</option>
						</select>
						</td>
						<td>
						duration: <select name="durationOfSession<%=i %>" id="durationOfSession<%=i %>">
								<option id="duration1<%=i %>" value="1" selected="selected">1 hour</option>
								<option id="duration2<%=i %>" value="2">2 hours</option>
								<option id="duration3<%=i %>" value="3">3 hours</option>
								<option id="duration4<%=i %>" value="4">4 hours</option>
								</select>
					</td>
				</tr>
				<%
					}
				%>
				</tbody></table>
				<table>	
					<tr><td><input type="hidden" id="username" name="username" value=<%=request.getUserPrincipal().getName() %>></input></td></tr>
				</table>
			</fieldset>
			<br/>
			<input type="submit" value="Submit!" />
		</div>
	</form>
	<script type="text/javascript">
		setCheckAndPostFormToServletWithCompleteFunc('newCourseForm', 'admin/checkCourse', checkForm,
			function(xhr) {
				if (xhr.status == 200) $('#allCoursesLink').click();
				else if (xhr.status == 201) loadPage('admin/newCourse.jsp?reason=invalid');
				else if (xhr.status == 202) loadPage('admin/newCourse.jsp?reason=mySessions');
				else if (xhr.status == 205) loadPage('admin/newCourse.jsp?courseId=invalid');
				else if (xhr.status == 206) loadPage('admin/newCourse.jsp?courseName=invalid');
				else loadPage('admin/newCourse.jsp?reason=' + (xhr.status-202));
			});
		
		createSessions();
	</script>
	
</div>
